package com.ruoyi.web.core.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.io.IOException;
import java.util.TimeZone;

/**
 * 返回json空值去掉null和""
 */
@Configuration
public class JacksonConfig {

    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        ObjectMapper objectMapper = builder.createXmlMapper(false).build();

        // 通过该方法对mapper对象进行设置，所有序列化的对象都将按改规则进行系列化
        // Include.Include.ALWAYS 默认
        // Include.NON_DEFAULT 属性为默认值不序列化
        // Include.NON_EMPTY 属性为 空（""） 或者为 NULL 都不序列化，则返回的json是没有这个字段的。这样对移动端会更省流量
        // Include.NON_NULL 属性为NULL 不序列化
        // objectMapper.setSerializationInclusion(Include.NON_NULL);

        // objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        // 允许单引号、允许不带引号的字段名称
//        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
//        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
        // 设置输入时忽略在JSON字符串中存在但Java对象实际没有的属性
//        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        // 空值处理为空串
        /*
         * objectMapper.getSerializerProvider().setNullValueSerializer(new
         * JsonSerializer<Object>(){
         *
         * @Override public void serialize(Object value, JsonGenerator jgen,
         * SerializerProvider provider) throws IOException, JsonProcessingException {
         * jgen.writeString(""); } });
         */
        // 进行HTML解码。
//        objectMapper.registerModule(new SimpleModule().addSerializer(String.class, new JsonSerializer<String>() {
//
//            @Override
//            public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
//                jgen.writeString(StringEscapeUtils.unescapeHtml4(value));
//            }
//        }));
        // 将long转化成String，避免长度过长出现经度丢失
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
//        simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        // 取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
        // objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
        // false);
        // objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        // 设置时区
//        objectMapper.setTimeZone(TimeZone.getDefault());

        // 如果java.time包下Json报错,添加如下两行代码
//        objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
//        objectMapper.registerModule(new JavaTimeModule());

        // objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,
        // ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        return objectMapper;
    }
}
